/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.ui.renderer;

import java.awt.Color;
import java.awt.Component;
import java.text.DateFormat;
import java.util.Date;

import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

import org.isqlviewer.history.CommandType;
import org.isqlviewer.history.HistoricalCommand;
import org.isqlviewer.swing.SwingUtilities;
import org.isqlviewer.swing.outline.JOutline;
import org.isqlviewer.ui.laf.EnhancedTableCellRenderer;

/**
 * Tree cell renderer for command history within the iSQL-Viewer program.
 * <p>
 * 
 * @author Mark A. Kobold &lt;mkobold at isqlviewer dot com&gt;
 * @version 1.0
 */
public class HistoryTreeCellRenderer extends DefaultTreeCellRenderer {

    private static final long serialVersionUID = 3928225003637084625L;
    private JOutline outline = null;

    public HistoryTreeCellRenderer(JOutline outline) {

        this.outline = outline;
        setTextSelectionColor(Color.WHITE);
        setClosedIcon(SwingUtilities.loadIconResource("day_of_month", 16));
        setBackgroundSelectionColor(EnhancedTableCellRenderer.selectedFocusedColor);
        setBorderSelectionColor(EnhancedTableCellRenderer.selectedFocusedColor);
    }

    @Override
    public Component getTreeCellRendererComponent(JTree t, Object o, boolean s, boolean x, boolean l, int r, boolean f) {

        boolean focusSelected = outline.getSelectedRow() == r;
        super.getTreeCellRendererComponent(t, o, focusSelected, x, l, r, focusSelected);

        if (focusSelected) {
            if (outline.hasFocus()) {
                setBackgroundSelectionColor(EnhancedTableCellRenderer.selectedFocusedColor);
                setBorderSelectionColor(EnhancedTableCellRenderer.selectedFocusedColor);
                setBackground(EnhancedTableCellRenderer.selectedFocusedColor);
                setForeground(Color.WHITE);
            } else {
                setBorderSelectionColor(EnhancedTableCellRenderer.selectedNotFocusedColor);
                setBackgroundSelectionColor(EnhancedTableCellRenderer.selectedNotFocusedColor);
                setBackground(EnhancedTableCellRenderer.selectedNotFocusedColor);
                setForeground(Color.BLACK);
            }
        } else {
            Color color = r % 2 == 0 ? EnhancedTableCellRenderer.evenRowColor : EnhancedTableCellRenderer.oddRowColor;
            setBackground(color);
            setBackgroundSelectionColor(color);
            setBackgroundNonSelectionColor(color);
            setForeground(Color.BLACK);
        }

        if (o instanceof HistoricalCommand) {
            HistoricalCommand historyNode = (HistoricalCommand) o;
            CommandType nodeType = historyNode.getType();
            String iconName = nodeType.getPreferredIcon();
            if (iconName != null && iconName.length() > 0) {
                setIcon(SwingUtilities.loadIconResource(iconName, 16));
            } else {
                setIcon(null);
            }
            setText(nodeType.name());
        } else if (o instanceof Date) {
            DateFormat format = DateFormat.getDateInstance(DateFormat.MEDIUM);
            setText(format.format((Date) o));
            setIcon(getClosedIcon());
        }
        return this;
    }
}
